if(Utils == undefined){
	var Utils = {};
}
/**
 * fix the IE's bug that the div can't cover the select, iframe and applet tag.
 * @param element
 * 			the html element that need to be showed on the top layer.
 */
Utils.hideShowCovered = function (element) {
	if(element == null){
		return;
	}
	if (!Prototype.Browser.IE && !Prototype.Browser.Opera)
		return;
	function getVisib(obj){
		var value = obj.style.visibility;
		if (!value) {
			if (document.defaultView && typeof (document.defaultView.getComputedStyle) == "function") { // Gecko, W3C
				value = '';
			} else if (obj.currentStyle) { // IE
				value = obj.currentStyle.visibility;
			} else
				value = '';
		}
		return value;
	};

	var tags = new Array("applet", "iframe", "select");
	var el = element;

	var p = {};
	var pos = Position.cumulativeOffset(el);
	p.x = pos[0];
	p.y = pos[1];
	var EX1 = p.x;
	var EX2 = el.offsetWidth + EX1;
	var EY1 = p.y;
	var EY2 = el.offsetHeight + EY1;

	for (var k = tags.length; k > 0; ) {
		var ar = document.getElementsByTagName(tags[--k]);
		var cc = null;

		for (var i = ar.length; i > 0;) {
			cc = ar[--i];

			var p = {};
			var pos = Position.cumulativeOffset(cc);
			p.x = pos[0];
			p.y = pos[1];
			var CX1 = p.x;
			var CX2 = cc.offsetWidth + CX1;
			var CY1 = p.y;
			var CY2 = cc.offsetHeight + CY1;

			if (this.hidden || (CX1 > EX2) || (CX2 < EX1) || (CY1 > EY2) || (CY2 < EY1)) {
				if (!cc.__msh_save_visibility) {
					cc.__msh_save_visibility = getVisib(cc);
				}
				cc.style.visibility = cc.__msh_save_visibility;
			} else {
				if (!cc.__msh_save_visibility) {
					cc.__msh_save_visibility = getVisib(cc);
				}
				cc.style.visibility = "hidden";
			}
		}
	}
};
Utils.doScroll = function(element, action){
	if(Prototype.Browser.IE){
		element.doScroll(action);
	}else{
		if(action == "scrollbarPageDown"){
			element.scrollTop = element.scrollTop + element.clientHeight;
		}else if(action == "scrollbarPageUp"){
			element.scrollTop = element.scrollTop - element.clientHeight;
		}
	}
	
}
Utils.highlightKeyword = function(keyword, str){
	var result = '';
	var t = '' + str;
	result = t.replace(keyword, "<span class='keywordHighlight'>" + keyword + "</span>");
	return result;
}
/**
 * 获取str的最后一个field
 * 如：str = person.name.firstName, 返回firstName
 */
Utils.getLastField = function(str){
	var result;
	var dotIndex = str.lastIndexOf(".");
	if(dotIndex != -1){
		result = str.substring(dotIndex + 1);
	}else{
		result = str;
	}
	return result;
}
/**
 * 获取str的第一个field
 * 如：str = person.name.firstName, 返回name.firstName
 * 如果没有,返回null
 */
Utils.getSubField = function(str){
	var result = null;
	var dotIndex = str.indexOf(".");
	if(dotIndex != -1){
		result = str.substring(dotIndex + 1);
	}else{
		result = str;
	}
	return result;
}
/**
 * 获取str的包名
 * 如：str = person.name.firstName, 返回person
 */
Utils.getPackageName = function(str){
	var result = null;
	if(str != undefined){
		var dotIndex = str.indexOf(".");
		if(dotIndex != -1){
			result = str.substring(0, dotIndex);
		}
	}
	return result;
}
/**
 * 从数组arr中删除element
 */
Utils.removeElementFromArray = function(arr, element){
	for(var i=0; i<arr.length; i++){
		if(arr[i] == element){
			arr.splice(i, 1);
		}
	}
}
Utils.RTrim = function(VALUE, w_spaces){
	if (w_spaces == undefined || w_spaces == null) {
		w_spaces = [];
		w_spaces.push(String.fromCharCode(32));
	}
//	var w_space = String.fromCharCode(32);
	var v_length = VALUE.length;
	var strTemp = "";
	if(v_length < 0){
		return"";
	}
	var iTemp = v_length -1;

	while(iTemp > -1){
		var ch = VALUE.charAt(iTemp);
		var match = false;
		for (var i = 0; i < w_spaces.length; i++) {
			if (ch == w_spaces[i]) {	
				match = true;
				break;			
			} 
		}
		
		if (!match) {
			strTemp = VALUE.substring(0,iTemp +1);
			break;
		}
		iTemp = iTemp-1;

	} //End While
	return strTemp;

} //End Function

Utils.LTrim = function(VALUE, w_spaces){
	if (w_spaces == undefined || w_spaces == null) {
		w_spaces = [];
		w_spaces.push(String.fromCharCode(32));
	}
//	var w_space = String.fromCharCode(32);
	if(v_length < 1){
		return"";
	}
	var v_length = VALUE.length;
	var strTemp = "";

	var iTemp = 0;

	while(iTemp < v_length){
		var ch = VALUE.charAt(iTemp);
		var match = false;
		for (var i = 0; i < w_spaces.length; i++) {
			if (ch == w_spaces[i]) {	
				match = true;
				break;			
			} 
		}
		
		if (!match) {
			strTemp = VALUE.substring(iTemp,v_length);
			break;
		}
		iTemp = iTemp + 1;
	} //End While
	return strTemp;
} //End Function

Utils.TrimSBCandDBC = function(TRIM_VALUE){
	if(TRIM_VALUE.length < 1){
		return"";
	}
	TRIM_VALUE = Utils.RTrim(TRIM_VALUE, [String.fromCharCode(32), '　']);
	TRIM_VALUE = Utils.LTrim(TRIM_VALUE, [String.fromCharCode(32), '　']);
	if(TRIM_VALUE==""){
		return "";
	}
	else{
		return TRIM_VALUE;
	}
} //End Function
/**
 * 显示ajax返回的数据中的提示信息
 * @param dataFromServer
 * 			从服务器返回的json对象
 */
Utils.showAjaxTip = function(dataFromServer){
	var msg = "";
	var actionMessages = dataFromServer[1];
	var actionErrors = dataFromServer[2];
	var fieldErrors = dataFromServer[4].fieldErrors;
	if(actionMessages.length > 0){
		msg += actionMessages.join("\r\n");
	}
	if(actionErrors.length > 0){
		msg += actionErrors.join("\r\n");
	}
	
	if(fieldErrors){
		for(p in fieldErrors){
			msg += fieldErrors[p] + "\r\n";
		}
	}
	if(msg != "")
		alert(msg);
}


/**
 * 把一个日期类型转换为一个字符串(不显示时间)
 * 显示格式：2008-08-08
 * @param date
 * 			要转换的日期(Date类型)
 * @return	
 * 			返回转换后的字符串
 */
Utils.getLongDate = function (date) {
	var d = date;
	var year = d.getFullYear().toString();
	var monthInt = d.getMonth() + 1;
	var month = monthInt.toString();
	if (month.length < 2) {
		month = "0" + month;
	}
	var day = d.getDate().toString();
	if (day.length < 2) {
		day = "0" + day;
	}
	return year + "-" + month + "-" + day;
}
//extend Array object
Array.prototype.indexOf = function(object){
	for(var i=0; i<this.length; i++){
		if(this[i] == object){
			return i;
		}
	}
	return -1;
}
Array.prototype.addAll = function(arr){
	for(var i=0; i<arr.length; i++){
		this.push(arr[i]);
	}
}

//extend Array object end

Utils.functionNames = ['onclick','ondblclick','getElementsByClassName','visible','toggle','hide','show','remove','update','replace','getHeight','classNames','hasClassName','addClassName','removeClassName','cleanWhitespace','empty','childOf','scrollTo','getStyle','setStyle','getDimensions','makePositioned','undoPositioned','makeClipping','undoClipping','fire','observe','stopObserving','insert','wrap','inspect','recursivelyCollect','ancestors','descendants','firstDescendant','immediateDescendants','previousSiblings','nextSiblings','siblings','match','up','down','previous','next','select','adjacent','identify','readAttribute','writeAttribute','getWidth','toggleClassName','descendantOf','getOpacity','setOpacity','cumulativeOffset','positionedOffset','absolutize','relativize','cumulativeScrollOffset','getOffsetParent','viewportOffset','clonePosition','getElementsBySelector','childElements','hasAttribute','_extendedByPrototype','morph','visualEffect','highlight','fade','appear','grow','shrink','fold','blindUp','blindDown','slideUp','slideDown','pulsate','shake','puff','squish','switchOff','dropOut','getInlineOpacity','forceRerendering','setContentZoom','collectTextNodes','collectTextNodesIgnoreClass','getStyles','onmouseover','onmouseout']; 
Utils.removeElementToRecoverMemory = function(element){
	var functionNames = Utils.functionNames;
	var t;
	for(var i=0; i<functionNames.length; i++){
		t = functionNames[i];
  		if(element[t] !== undefined){
  			element[t] = null;
  		}
  	}		
}
Utils.removeNode = function(node){
	var childNodes = node.childNodes;
	
	if(childNodes.length == 0){
		if(node.tagName == undefined){
			//debugger;
		}
		Utils.removeElementToRecoverMemory(node);
		node.parentNode.removeChild(node);
		//Logger.debug("node: " + node + "," + node.tagName + ", " + node.id);
	}else{
		for(var i=0; i<childNodes.length; i++){
			Utils.removeNode(childNodes[i]);
			i--;
		}
	}
	
}
